﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using bs_ShowRoom;
namespace ShowRoom.admin
{
    public partial class AddMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetMenu();
            }
        }

        #region "Thông tin menu"
        private void GetMenu()
        {
            string mn_id = "0";
            if (Request.QueryString["mn_id"] != "" && Request.QueryString["mn_id"] != null)
            {
                mn_id = Request.QueryString["mn_id"].ToString().Trim();

                string mn_nm ="";
                string status = "";
                DataTable dt_ = GetMenuAdmin(mn_id, mn_nm, status);
                if (dt_.Rows.Count > 0)
                {
                    txtTen.Text = dt_.Rows[0]["mn_nm"].ToString().Trim();
                    txtLink.Text = dt_.Rows[0]["link"].ToString().Trim();
                    ddlActive.SelectedValue = dt_.Rows[0]["status"].ToString().Trim();
                }
            }
        }
        private DataTable GetMenuAdmin(string mn_id, string mn_nm, string status)
        {
            bs_menu mybs = new bs_menu(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("mn_id", mn_id);
            myMsg.CreateFirstNode("mn_nm", mn_nm);
            myMsg.CreateFirstNode("status", status);
            DataSet ds = mybs.DoReturnDataSet("GETMENUADMINBYSEARCH", myMsg.GetXmlStr, "");

            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            return dt;

        }
        #endregion
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string mn_id="0";
            string mn_nm=txtTen.Text.ToString().Trim();
            string link=txtLink.Text.ToString().Trim();
            string status=ddlActive.SelectedValue.Trim();
            string upd_id=Session["user_id"].ToString().Trim();
            string Type = "0";
            if (Request.QueryString["mn_id"] != "" && Request.QueryString["mn_id"] != null)
            {
                Type = "1";
                mn_id = Request.QueryString["mn_id"].ToString().Trim();
            }

            string ok = SaveData(mn_id, mn_nm, link, status, upd_id, Type);
          if (ok == "")
            {
                string Mes="";
                if(Type == "0")
                    Mes="Thêm mới menu thành công !";
                else
                    Mes = "Cập nhật menu thành công !";
                string url = "MenuManager.aspx";
               // Response.Write("<script>alert('Quý khách đã đăng ký thành công tài khoản tại showroommypham.com !');window.location='login.aspx?&name=" + txtuser.Text.ToString().Trim() + "'</script>");
                ScriptFunction.ShowMsgTranOpennerUrl_ScriptManager(Mes, Page, url);
            }
            else
            {
                Response.Write("<script>alert('Đả có lỗi xãy ra trong quá trình thêm !')</script>");
                return;
            }
        }
        #region "Save Data"
        public string SaveData(string mn_id, string mn_nm, string link, string status, string upd_id, string Type)
        {
            bs_menu myBs = new bs_menu(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("mn_id", mn_id);
            myMsg.CreateFirstNode("mn_nm", mn_nm);
            myMsg.CreateFirstNode("link", link);
            myMsg.CreateFirstNode("status", status);
            myMsg.CreateFirstNode("upd_id", upd_id);
            myMsg.CreateFirstNode("Type", Type);
            string strXML = myMsg.GetXmlStr;
            string strReturn = myBs.DoReturnStr("INSUPDMENUADMIN", strXML, "");
            myMsg.LoadXml(strReturn);
            return myMsg.Query("Err");
        }

        #endregion

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.close();</script>");
        }
    }
}